package com.it.controller;

import com.it.tools.CosFileupload;
import com.it.tools.NormalException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 羡羡
 */
@Controller
@SessionAttributes({"imglj"})
public class HeadSwitchController {

    @RequestMapping("/file")
    public String fileup(MultipartFile[] multipart, ModelMap map) throws NormalException {
        for(MultipartFile file : multipart){
            //文件name
            String fname=file.getOriginalFilename();
            //得到时间戳
            Long time=System.currentTimeMillis();
            //找到.
            int wz=fname.lastIndexOf(".");
            //后缀名
            String filna=fname.substring(wz+1);
            System.out.println("后缀名："+filna);
            //图片名字设置时间戳
            String pna=time+"."+filna;
            System.out.println("图片名："+pna);
            try {
                CosFileupload.upfile(file.getInputStream(),pna);
                map.put("imglj","https://sls-study-cloud-1301165591.cos.ap-guangzhou.myqcloud.com/"+pna);
            }catch (Exception e) {
                throw new NormalException(e.getMessage());
            }
        }
        return "redirect:HeadToSwitch.jsp";
    }
}
